package com.kim.nidoya.net.business;

import java.io.IOException;
import java.io.InputStream;

import org.json.JSONObject;

import android.util.Log;

import com.kim.net.NetCallback;
import com.kim.net.NetEntity;
import com.kim.nidoya.utils.Utils;

public class NetCallbackImplBase implements NetCallback {

	public interface Notify {
		void notifyNetCallback(NetEntity netEntity);

		void notifyError(int httpResponseCode, String errorMsg);
	}

	final Notify notify;

	public NetCallbackImplBase(Notify notify) {
		this.notify = notify;
	}

	@Override
	public void onSuccess(int httpResponseCode, InputStream inputStream,
			NetEntity netEntity, boolean gzip) {
		try {
			String result = Utils.convertInputStream2String(inputStream, gzip);
			Log.d("NetCallbackImplBase", "resultInStr:" + result);
			netEntity.receiveData = result;
			JSONObject jsonObject = Utils.string2JSON(result);
			netEntity.setResult(jsonObject);
			notify.notifyNetCallback(netEntity);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onError(int httpResponseCode, String errorMsg) {
		notify.notifyError(httpResponseCode, errorMsg);
	}

	@Override
	public void onOtherResponseCode(int httpResponseCode,
			InputStream inputStream, NetEntity netEntity, boolean gzip) {
		
	}

}
